package com.lin.controller;

import com.lin.common.utils.DateUtils;
import com.lin.controller.req.ExecutePaymentsReqMsg;
import com.lin.controller.req.OrderPayReqMsg;
import com.lin.pay.paypal.v1.PaymentService;
import com.lin.pay.paypal.v1.vo.CreatePaymentReqVo;
import com.lin.pay.paypal.v2.CaptureOrder;
import com.lin.pay.paypal.v2.CapturesGet;
import com.lin.pay.paypal.v2.CreateOrder;
import com.lin.pay.paypal.v2.OrderService;
import com.lin.pay.paypal.v2.RefundOrder;
import com.lin.pay.paypal.v2.RefundsGet;
import com.lin.pay.paypal.v2.dto.orders.CreateOrderDto;
import com.lin.pay.paypal.v2.dto.orders.Refund;
import com.lin.service.chats.ChatsServiceImpl;
import com.paypal.payments.Capture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * V2的关键接口
 * http://localhost:3000/api/createOrder        /v2/checkout/orders
 * https://www.sandbox.paypal.com/graphql?UpdateClientConfig
 * https://www.sandbox.paypal.com/graphql?GetCheckoutDetails
 * https://www.sandbox.paypal.com/graphql?UpgradeFacilitatorAccessToken
 * http://localhost:3000/api/getOrderDetails    /v2/checkout/orders/{}
 */
@RestController
@RequestMapping("/unverify")
public class paypalController {
    Logger log = LoggerFactory.getLogger(paypalController.class);
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private OrderService orderService;

    @RequestMapping("/payapl/createPayment")
    public String createPayment(@RequestBody CreatePaymentReqVo reqMsg) throws IOException {
        return paymentService.createPayment(reqMsg);
    }

    @RequestMapping("/payapl/executePayments")
    public String executePayments(@RequestBody ExecutePaymentsReqMsg reqMsg) throws IOException {
        return paymentService.executePayments(reqMsg.getPaymentID(), reqMsg.getPayerID());
    }

    /////////////////////////////////////////////paypal V2  //////////////////////////////
    @PostMapping("/payapl/v2/create-order")
    public CreateOrderDto v2createOrder(@RequestBody OrderPayReqMsg reqMsg) throws IOException {
        log.info("00000000000::::{},{}",reqMsg.getTrxAmt(),reqMsg.getCurrency());
        return orderService.createOrder("100", "USD");
    }

    @GetMapping("/payapl/v2/order-detail/{id}")
    public CreateOrderDto v2orderDetail(@PathVariable String id) throws IOException {
        return orderService.orderDetail(id);
    }
//    @RequestMapping("/payapl/v2/patch-order")
//    public CreateOrderDto v2patchOrder(@RequestBody Object reqMsg) throws IOException {
//        return orderService.patchOrder("100", "USD");
//    }

    @PostMapping("/payapl/v2/capture-payment/{id}")
    public Capture v2capturePayment(@PathVariable String id) throws IOException {
        return orderService.capturePayment(id);
    }

    @PostMapping("/payapl/v2/capture-refund/{id}")
    public Refund v2captureRefund(@PathVariable String id, @RequestBody Object obj) throws IOException {
        return orderService.captureRefund(id, "100", "USD");
    }


    /////////////////////////////////////////////paypal V2  //////////////////////////////

    @PostMapping("/payapl/createOrder")
    public String createOrder(@RequestBody CreatePaymentReqVo reqMsg) throws IOException {
        CreateOrder co = new CreateOrder();
        co.createOrder();
        return null;
    }

    @RequestMapping("/payapl/CaptureOrder")
    public String captureOrder() throws IOException {
        CaptureOrder co = new CaptureOrder();
        co.captureOrder(DateUtils.fullDateFormat());
        return null;
    }

    @RequestMapping("/payapl/capturesGet")
    public String capturesGet() throws IOException {
        CapturesGet co = new CapturesGet();
        co.testCapturesGetRequest("扣款id， CaptureOrder生成");
        return null;
    }

    @RequestMapping("/payapl/refundOrder")
    public String refundOrder() throws IOException {
        RefundOrder co = new RefundOrder();
        co.refundOrder("订单ID");
        return null;
    }

    @RequestMapping("/payapl/refundsGet")
    public String refundsGet() throws IOException {
        RefundsGet co = new RefundsGet();
        co.testRefundsGetRequest("退款id RefundOrder生成");
        return null;
    }
}
